import pandas as pd
import os
import json
from datetime import datetime

def debug_all_data_types():
    """全面调试所有数据类型"""
    
    data_types = ['delivery', 'workload', 'defect']
    results = {}
    
    for data_type in data_types:
        print(f"\n🔍 开始调试 {data_type} 数据...")
        
        folder = f'uploads/{data_type}'
        if not os.path.exists(folder):
            print(f"❌ {folder} 目录不存在")
            continue
            
        files = [f for f in os.listdir(folder) if f.endswith(('.xls', '.xlsx'))]
        if not files:
            print(f"❌ {data_type} 目录中未找到Excel文件")
            continue
            
        filepath = os.path.join(folder, files[0])
        print(f"📁 正在调试文件: {filepath}")
        
        try:
            # 读取文件
            if filepath.endswith('.xls'):
                df = pd.read_excel(filepath, engine='xlrd')
            else:
                df = pd.read_excel(filepath, engine='openpyxl')
            
            print(f"✅ 文件读取成功，形状: {df.shape}")
            
            # 显示列信息
            print(f"📊 {data_type} 数据列名:")
            for i, col in enumerate(df.columns):
                print(f"  {i}: '{col}'")
            
            # 显示数据样本
            print(f"📋 {data_type} 前3行数据:")
            print(df.head(3).to_string())
            
            results[data_type] = {
                'file': filepath,
                'shape': df.shape,
                'columns': list(df.columns),
                'sample': df.head(3).to_dict('records')
            }
            
        except Exception as e:
            print(f"❌ {data_type} 文件读取失败: {e}")
            results[data_type] = {'error': str(e)}
    
    # 保存调试结果
    with open('debug_all_data.json', 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=2)
    
    print(f"\n💾 所有调试结果已保存到 debug_all_data.json")

if __name__ == "__main__":
    debug_all_data_types()